<?php 
class GoodsAction extends Action{
	public function index(){
		$this->display();
	}
	public function data_list(){
		$Goods = M('goods');
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$offset = ($page-1)*$rows;
		$row = mysql_fetch_assoc(mysql_query("select count(*) As Total from `goods`"));
		$result['total'] = $row['Total'];
		$items = $Goods->order('id DESC')->limit("$offset,$rows")->select();
		$result["rows"] = $items;
		echo json_encode($result);
	}
	public function edit(){
		$Goods   =   M('Goods');
		$id=$_GET['id'];
	    $data = $Goods->find($id);
	    if($data) {
	        $this->data = $data;
	    }else{
	        $this->error('数据错误');
	    }
	    //获取所有供应商列表
	    $supplier = M('supplier');
	    //获取供应商列表并转换成数组
	    $query_supplier = $supplier->field('id,name')->select();
	    foreach ($query_supplier as $key=>$value){
	    	$supplier_id[] = $value['id'];
	    	$supplier_name[] = $value['name'];
	    }
	    $this->suppier_id_name_arr = array_combine($supplier_id,$supplier_name);
	    //获取产品与供应商中间表并转换成数组
	    $goods_supplier = M('goods_supplier');
	    $condition['goods_id'] = $id;
	    $goods_supplier = $goods_supplier->field('supplier_id')->where($condition)->select();
	    foreach ($goods_supplier as $key=>$value){
	    	foreach ($value as $k){
	    		$suppier_id_arr[] .= $k;
	    	}
	    }
	    $this->suppier_id_arr = $suppier_id_arr;
	    $this->display();
	}
	public function add(){
		$supplier = M('supplier');
	    //获取供应商列表并转换成数组
	    $query_supplier = $supplier->field('id,name')->select();
	    foreach ($query_supplier as $key=>$value){
	    	$supplier_id[] = $value['id'];
	    	$supplier_name[] = $value['name'];
	    }
	    $this->suppier_id_name_arr = array_combine($supplier_id,$supplier_name);
		$this->display();
	}
	public function data_remove(){
		$Goods = M('goods');
		$id = intval($_REQUEST['id']);
		$result = $Goods->where("id=$id")->delete();
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Some errors occured.'));
		}
	}
	public function data_update(){
		$Goods = M('goods');
		$id = intval($_REQUEST['id']);
		$title = $_REQUEST['title'];
		$purchasing_price = $_REQUEST['purchasing_price'];
		$retail_price = $_REQUEST['retail_price'];
		$stock = $_REQUEST['stock'];
		$content = $_REQUEST['content'];
		$picture = $_REQUEST['picture_data'];
		$sql = "update goods set title='$title',purchasing_price='$purchasing_price',retail_price='$retail_price',stock='$stock',content='$content' where id=$id";
		$result = @mysql_query($sql);
		//先删除的插入实现更新功能
		$Goods_supplier = M('goods_supplier');
		mysql_query("delete from goods_supplier where goods_id='".$id."'");
		$suppliers = $_REQUEST[supplier];
		foreach ($suppliers as $value){
			$query = mysql_query("insert into goods_supplier (goods_id,supplier_id)"."values('$id','$value')");
		}
		//生成图片
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  'uploads/goods/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功
			$this->success('上传成功！');
		}
		@unlink($upload->savePath.$picture);
		//取得成功上传的文件信息
		$info = $upload->getUploadFileInfo();
		$data['picture'] = $info[0]['savename'];
		mysql_query("update goods set picture='".$data['picture']."' where id='".$id."'");
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Some errors occured.'));
		}
	}
	public function data_save(){
			$Goods = M('goods');
			$result = false;
			$sql="insert into goods (id,title,stock,purchasing_price,retail_price,content,date)"."values('','$_POST[title]','$_POST[stock]','$_POST[purchasing_price]','$_POST[retail_price]','$_POST[content]',CURDATE())";
			$result = mysql_query($sql);
			$id=mysql_insert_id();
			//提交数据到产品表供应商中间表
			if(isset($_POST[supplier]) && $_POST[supplier]!=''){
				$suppliers = $_POST[supplier];
				foreach ($suppliers as $value){
					$query = mysql_query("insert into goods_supplier (goods_id,supplier_id)"."values('$id','$value')");
				}
			}
			//设置SKU
			$sku=$id+10000;
			$Goods->where("id=$id")->setField("sku","$sku");
			//生成图片
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  'uploads/goods/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功
			$this->success('上传成功！');
		}
		@unlink($upload->savePath.$picture);
		//取得成功上传的文件信息
		$info = $upload->getUploadFileInfo();
		$data['picture'] = $info[0]['savename'];
		mysql_query("update goods set picture='".$data['picture']."' where id='".$id."'");
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Some errors occured.'));
		}
		   //判断表单提交是否成功
			if ($result == "true"){
				echo json_encode(array('success'=>true));
			} else {
		
				echo json_encode(array('msg'=>'had errors occured. ' . $result));
			}
	}
}
?>